package com.tanhua.server.controller;

import com.tanhua.model.dto.CommentDto;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentsService commentsService;

    //分页查询评论列表
    @GetMapping
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   String movementId) {

        PageResult pr = commentsService.findComments(movementId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    //对动态发布评论
    @PostMapping
    public ResponseEntity pushComments(@RequestBody CommentDto commentDto) {
        //1.接收动态Id和评论内容
        String comment = commentDto.getComment();
        String movementId = commentDto.getMovementId();
        //调用service
        commentsService.pushComments(comment,movementId);
        return ResponseEntity.ok(null);
    }


    /**
     * 对评论点赞
     * @param commentId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String commentId){
        //点赞返回点赞总记录数
        Integer likeCount = commentsService.likeComment(commentId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 对评论取消点赞
     * @param commentId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String commentId){
        //取消点赞后,返回点赞总记录数
        Integer likeCount = commentsService.disLikeComment(commentId);
        return ResponseEntity.ok(likeCount);
    }

}
